package com.kensure.shike.baobei.service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.PageInfo;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.dao.SKBaobeiDao;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.model.SKBaobeiSale;
import com.kensure.shike.baobei.model.SKType;
import com.kensure.shike.baobei.query.SKBaobeiHTQuery;
import com.kensure.shike.baobei.query.SKBaobeiQuery;
import com.kensure.shike.bsc.service.SKBanShiChuService;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.changshang.service.ChangShangService;
import com.kensure.shike.order.service.SKOrderService;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 宝贝信息服务实现类
 * 
 * @author fankd created on 2019-6-10
 * @since
 */
@Service
public class SKBaobeiService extends JSBaseService {

	@Resource
	private SKBaobeiDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKBaobeiTPService sKBaobeiTPService;
	@Resource
	private SKBaobeiDetailService sKBaobeiDetailService;
	@Resource
	private SKBaobeiYansePicService sKBaobeiYansePicService;
	@Resource
	private SKOrderService sKOrderService;
	@Resource
	private ChangShangService changShangService;
	@Resource
	private SKBanShiChuService sKBanShiChuService;
	@Resource
	private SKTypeService sKTypeService;
	@Resource
	private SKUserService sKUserService;

	public SKBaobei selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKBaobei> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKBaobei> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKBaobei obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_baobei"));
		if (obj.getStatus() == null) {
			obj.setStatus(0);
		}
		if (obj.getKjcs() == null) {
			obj.setKjcs(0);
		}
		if (obj.getSalenum() == null) {
			obj.setSalenum(0);
		}
		if (obj.getRenqi() == null) {
			obj.setRenqi(0);
		}
		return dao.insert(obj);
	}

	public boolean update(SKBaobei obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	/**
	 * 前端商品展现
	 */
	public List<SKBaobei> qdlist(SKBaobeiQuery query, PageInfo page) {
		Map<String, Object> parameters = MapUtils.genMap("status", 9, "is_del", 0, "orderby", "disorder,publish_time desc");
		MapUtils.putPageInfo(parameters, page);
		if (StringUtils.isNotBlank(query.getTitle())) {
			parameters.put("codeTitleLike", query.getTitle());
		}

		if (query.getTypeid() != null) {
			parameters.put("typeid", query.getTypeid());
		}
		if (query.getMinprice() != null) {
			parameters.put("startSalePrice", query.getMinprice());
		}
		if (query.getMaxprice() != null) {
			parameters.put("endSalePrice", query.getMaxprice());
		}
		return selectByWhere(parameters);
	}

	/**
	 * 详细商品
	 */
	public SKBaobei selectFull(Long id) {
		SKBaobei baobei = selectOne(id);
		baobei.setPics(sKBaobeiTPService.getList(id));
		baobei.setDetail(sKBaobeiDetailService.selectOne(id));
		baobei.setYansePics(sKBaobeiYansePicService.getList(id));
		return baobei;
	}

	/**
	 * 后端商品展现
	 */
	public List<SKBaobei> htlist(SKBaobeiHTQuery query, PageInfo pageinfo) {
		Map<String, Object> parameters = MapUtils.bean2Map(query, true);
		parameters.put("orderby", "id desc");
		MapUtils.putPageInfo(parameters, pageinfo);
		List<SKBaobei> list = selectByWhere(parameters);
		if ("1".equals(query.getNeedJx())) {
			for (SKBaobei bb : list) {
				SKBaobeiSale sale = new SKBaobeiSale();
				Long totalSaleNum = (Long) sKOrderService.saleNum(bb.getId()).get("sumbbnum");
				sale.setTotalSaleNum(totalSaleNum);
				bb.setsKBaobeiSale(sale);
			}
		}
		for (SKBaobei bb : list) {
			ChangShang changshang = changShangService.selectOne(bb.getSjid());
			bb.setChangshang(changshang);
		}

		return list;
	}

	/**
	 * 后端商品展现数量
	 */
	public Long htlistCount(SKBaobeiHTQuery query) {
		Map<String, Object> parameters = MapUtils.bean2Map(query, true);
		return selectCountByWhere(parameters);
	}

	/**
	 * 商家端商品展现
	 */
	public List<SKBaobei> sjlist(SKBaobeiHTQuery query, PageInfo pageinfo) {
		Map<String, Object> parameters = MapUtils.bean2Map(query, true);
		parameters.put("orderby", "id desc");
		MapUtils.putPageInfo(parameters, pageinfo);
		List<SKBaobei> list = selectByWhere(parameters);
		return list;
	}

	/**
	 * 商家端商品展现数量
	 */
	public Long sjlistCount(SKBaobeiHTQuery query) {
		Map<String, Object> parameters = MapUtils.bean2Map(query, true);
		return selectCountByWhere(parameters);
	}

	/**
	 * 后端商品保存
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void save(SKBaobei baobei) {
		baobei.setIsDel(0);
		baobei.setPinpai("1");
		if (CollectionUtils.isEmpty(baobei.getPics())) {
			BusinessExceptionUtil.threwException("请上传轮播图片");
		}
		baobei.setPicurl(baobei.getPics().get(0).getUrl());
		if (baobei.getSjid() == null) {
			BusinessExceptionUtil.threwException("商家必须选择");
		}
		ChangShang changshang = changShangService.selectOne(baobei.getSjid());
		if (changshang == null) {
			BusinessExceptionUtil.threwException("商家必须选择!!");
		}

		baobei.setFenrunTypeid(1);
		if (baobei.getFenrunTypeid() == 1) {
			if (baobei.getVip1Fenrui1() >= 0.5) {
				BusinessExceptionUtil.threwException("分润比例不正确！");
			}
			if (baobei.getVip2Fenrui2() >= 0.5) {
				BusinessExceptionUtil.threwException("分润比例不正确！");
			}
		}
		if(baobei.getTjr1() != null){
			SKUser u1 = sKUserService.selectOne(baobei.getTjr1());
			if(u1 == null){
				BusinessExceptionUtil.threwException("找不到该直接推荐人！");
			}
		}
		if(baobei.getTjr2() != null){
			SKUser u2 = sKUserService.selectOne(baobei.getTjr2());
			if(u2 == null){
				BusinessExceptionUtil.threwException("找不到该间接推荐人！");
			}
		}
		
		

		if (baobei.getId() == null) {
			insert(baobei);
		} else {
			update(baobei);
		}
		// 图片
		sKBaobeiTPService.add(baobei);
		// 详情
		sKBaobeiDetailService.add(baobei);
		// tag标签
		// sKBaobeiTagService.add(baobei);
		// 颜色
		sKBaobeiYansePicService.add(baobei);
		// sku
		// sKBaobeiSkuService.add(baobei);
	}

	/**
	 * 后端商品上架
	 */
	public void shangjia(Long id) {
		SKBaobei baobei = selectFull(id);
		if (baobei.getStatus() == 9) {
			BusinessExceptionUtil.threwException("商品已经上架");
		}
		baobei.setStatus(9);
		baobei.setPublishTime(new Date());
		update(baobei);
	}

	/**
	 * 后端商品下架
	 */
	public void xiajia(Long id) {
		SKBaobei baobei = selectFull(id);
		baobei.setStatus(-1);
		update(baobei);
	}

	/**
	 * 前端首页问题
	 */
	public List<SKType> sylist() {
		List<SKType> list = sKTypeService.selectByPid(0L);
		for (SKType type : list) {
			PageInfo page = new PageInfo(1, 12);
			SKBaobeiQuery query = new SKBaobeiQuery();
			query.setTypeid(type.getId().intValue());
			List<SKBaobei> bblist = qdlist(query, page);
			type.setBblist(bblist);
			type.setUrl("/pages/huopin/index?typeid=" + type.getId());
		}
		return list;
	}

}
